package top.zywork.service.impl;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import top.zywork.common.BeanUtils;
import top.zywork.common.DateUtils;
import top.zywork.common.TransactionNoGenerator;
import top.zywork.dao.*;
import top.zywork.dos.*;
import top.zywork.dto.GoodsOrderDTO;
import top.zywork.dto.GoodsOrderLogisticsDTO;
import top.zywork.dto.PagerDTO;
import top.zywork.enums.AccountStatusEnum;
import top.zywork.enums.DeliverTypeEnum;
import top.zywork.enums.GoodsOrderStatusEnum;
import top.zywork.enums.SysConfigEnum;
import top.zywork.query.GoodsOrderQuery;
import top.zywork.query.UserGoodsOrderItemQuery;
import top.zywork.security.SecurityUtils;
import top.zywork.service.*;
import top.zywork.vo.*;
import top.zywork.vo.goods.GoodsOrderAutoJobConfig;

import javax.annotation.PostConstruct;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * GoodsOrderServiceImpl服务接口实现类<br/>
 *
 * 创建于2019-07-04<br/>
 *
 * @author http://zywork.top 王振宇
 * @version 1.0
 */
@Service(value = "goodsOrderService")
@Slf4j
public class GoodsOrderServiceImpl extends AbstractBaseService implements GoodsOrderService {

    private GoodsOrderDAO goodsOrderDAO;

    private GoodsOrderItemDAO goodsOrderItemDAO;

    private GoodsOrderLogisticsDAO goodsOrderLogisticsDAO;

    private GoodsOrderLogisticsService goodsOrderLogisticsService;

    private GoodsInfoDAO goodsInfoDAO;

    private GoodsSkuDAO goodsSkuDAO;

    private AccountDetailDAO accountDetailDAO;

    private PlatformAccountDetailDAO platformAccountDetailDAO;

    private GoodsOrderAccountDAO goodsOrderAccountDAO;

    private GoodsOrderAccountDetailDAO goodsOrderAccountDetailDAO;

    private ShippingAddressDAO shippingAddressDAO;

    private DistributionService distributionService;

    private SysConfigService sysConfigService;

    private UserGoodsOrderItemDAO userGoodsOrderItemDAO;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Map<Long, String> saveOrders(String agentRole, List<GoodsOrderAndItemInVO> goodsOrderAndItemInVOList) {
        Map<Long, String> saveRowsMap = new HashMap<>();
        boolean allSkuStockEnough = true;
        for (GoodsOrderAndItemInVO goodsOrderAndItemInVO : goodsOrderAndItemInVOList) {
            GoodsOrderDO goodsOrderDO = BeanUtils.copy(goodsOrderAndItemInVO, GoodsOrderDO.class);
            goodsOrderDO.setUserId(SecurityUtils.getJwtUser().getUserId());
            goodsOrderDO.setOrderNo(TransactionNoGenerator.generateOrderNo());
            // 购买分销商商品，需要记录分销商角色到订单中
            if (!"none".equals(agentRole)) {
                goodsOrderDO.setShopRemark("[购买分销商商品:" + goodsOrderDO.getOrderNo() + ":" + agentRole + "]");
                goodsOrderDO.setRemark(StringUtils.isEmpty(goodsOrderDO.getRemark()) ? "[购买分销商商品:" + goodsOrderDO.getOrderNo() + ":" + agentRole + "]"
                        : goodsOrderDO.getRemark() + "，[购买分销商商品:" + goodsOrderDO.getOrderNo() + ":" + agentRole + "]");
            }
            List<GoodsOrderItemDO> goodsOrderItemDOList = BeanUtils.copy(goodsOrderAndItemInVO.getGoodsOrderItemInVOList(), GoodsOrderItemDO.class);
            goodsOrderDAO.save(goodsOrderDO);
            saveGoodsOrderLogistics(goodsOrderDO.getId(), goodsOrderAndItemInVO.getDeliverType(), goodsOrderAndItemInVO.getAddressId(), goodsOrderAndItemInVO.getShopId());
            // 订单创建时只会保存订单账目信息
            saveGoodsOrderAccount(goodsOrderDO.getUserId(), goodsOrderDO.getShopId(), goodsOrderDO.getId(), goodsOrderDO.getTotalAmount(),
                    goodsOrderDO.getPayAmount(), goodsOrderDO.getDiscountAmount(), goodsOrderAndItemInVO.getPlatDiscountAmount(), goodsOrderAndItemInVO.getShopDiscountAmount());
            if (saveRowsMap.containsKey(0L)) {
                saveRowsMap.put(0L, saveRowsMap.get(0L) + "," + goodsOrderDO.getId() + "-" + goodsOrderDO.getOrderNo());
            } else {
                saveRowsMap.put(0L, goodsOrderDO.getId() + "-" + goodsOrderDO.getOrderNo());
            }
            Map<Long, Integer> quantityMap = new HashMap<>();
            boolean skuStockEnough = true;
            for (GoodsOrderItemDO goodsOrderItemDO : goodsOrderItemDOList) {
                int updateRow = goodsSkuDAO.updateStock(goodsOrderItemDO);
                saveRowsMap.put(goodsOrderItemDO.getGoodsSkuId(), updateRow + "");
                if (updateRow == 0) {
                    allSkuStockEnough = false;
                    skuStockEnough = false;
                }
                goodsOrderItemDO.setOrderId(goodsOrderDO.getId());
                Long goodsId = goodsOrderItemDO.getGoodsId();
                if (quantityMap.containsKey(goodsId)) {
                    quantityMap.put(goodsId, quantityMap.get(goodsId) + goodsOrderItemDO.getQuantity());
                } else {
                    quantityMap.put(goodsId, goodsOrderItemDO.getQuantity());
                }
            }
            if (skuStockEnough) {
                // 如果sku库存足，则更新库存后保存订单项
                goodsOrderItemDAO.saveBatch(BeanUtils.copyListObj(goodsOrderItemDOList, GoodsOrderItemDO.class));
                // 并且更新每个商品的销量
                List<GoodsInfoDO> goodsInfoDOList = new ArrayList<>();
                for (Map.Entry<Long, Integer> entry : quantityMap.entrySet()) {
                    goodsInfoDOList.add(GoodsInfoDO.builder().id(entry.getKey()).saleCount((long) entry.getValue()).build());
                }
                goodsInfoDAO.updateSaleCountBatch(goodsInfoDOList);
            }
        }
        if (!allSkuStockEnough) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        }
        return saveRowsMap;
    }

    private void saveGoodsOrderLogistics(Long orderId, String deliverType, Long addressId, Long shopId) {
        Object addressObj = shippingAddressDAO.getById(addressId);
        if (addressObj != null) {
            ShippingAddressDO shippingAddressDO = (ShippingAddressDO) addressObj;
            GoodsOrderLogisticsDO goodsOrderLogisticsDO = GoodsOrderLogisticsDO.builder()
                    .id(orderId)
                    .shopId(shopId)
                    .deliverType(StringUtils.isEmpty(deliverType) ? DeliverTypeEnum.NORMAL_LOGISTICS.getValue() : deliverType)
                    .realName(shippingAddressDO.getRealName())
                    .phone(shippingAddressDO.getPhone())
                    .province(shippingAddressDO.getProvince())
                    .city(shippingAddressDO.getCity())
                    .district(shippingAddressDO.getDistrict())
                    .addressAlias(shippingAddressDO.getAddressAlias())
                    .address(shippingAddressDO.getAddress())
                    .area(shippingAddressDO.getArea()).build();
            goodsOrderLogisticsDAO.save(goodsOrderLogisticsDO);
        }
    }

    private void saveGoodsOrderAccount(Long userId, Long shopId, Long orderId, BigDecimal totalAmount, BigDecimal payAmount,
                                   BigDecimal discountAmount, BigDecimal platDiscountAmount, BigDecimal shopDiscountAmount) {
        // 订单账目，店铺分账金额等于用户实付金额+平台优惠金额
        GoodsOrderAccountDO goodsOrderAccountDO = GoodsOrderAccountDO.builder().userId(userId).shopId(shopId).orderId(orderId)
                .transactionNo("未付款").totalAmount(totalAmount).payAmount(payAmount)
                .discountAmount(discountAmount).platDiscountAmount(platDiscountAmount).shopDiscountAmount(shopDiscountAmount)
                .shopAmount(payAmount.add(platDiscountAmount)).build();
        goodsOrderAccountDAO.save(goodsOrderAccountDO);
        // 订单多个账目详情，主要用于记录优惠情况，后期实现
        GoodsOrderAccountDetailDO goodsOrderAccountDetailDO = GoodsOrderAccountDetailDO.builder().userId(userId).shopId(shopId)
                .orderId(orderId).orderAccountId(goodsOrderAccountDO.getId()).build();
        // goodsOrderAccountDetailDAO.saveBatch();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int cancelOrder(UserGoodsOrderExVO userGoodsOrderExVO) {
        List<UserGoodsOrderItemVO> userGoodsOrderItemVOList = userGoodsOrderExVO.getUserGoodsOrderItemVOList();
        List<GoodsOrderItemDO> goodsOrderItemDOList = new ArrayList<>();
        Map<Long, Integer> quantityMap = new HashMap<>();
        for (UserGoodsOrderItemVO userGoodsOrderItemVO : userGoodsOrderItemVOList) {
            int quantity = userGoodsOrderItemVO.getGoodsOrderItemQuantity();
            goodsOrderItemDOList.add(GoodsOrderItemDO.builder().goodsSkuId(userGoodsOrderItemVO.getGoodsOrderItemGoodsSkuId()).quantity(quantity).build());
            Long goodsId = userGoodsOrderItemVO.getGoodsOrderItemGoodsId();
            if (quantityMap.containsKey(goodsId)) {
                quantityMap.put(goodsId, quantityMap.get(goodsId) + quantity);
            } else {
                quantityMap.put(goodsId, quantity);
            }
        }
        Long orderId = userGoodsOrderExVO.getGoodsOrderId();
        // 更新商品库存
        if (goodsOrderItemDOList.size() > 0) {
            goodsSkuDAO.updateStockBatch(goodsOrderItemDOList);
        }
        List<GoodsInfoDO> goodsInfoDOList = new ArrayList<>();
        for (Map.Entry<Long, Integer> entry : quantityMap.entrySet()) {
            goodsInfoDOList.add(GoodsInfoDO.builder().id(entry.getKey()).saleCount((long) -entry.getValue()).build());
        }
        // 更新商品销量
        if (goodsInfoDOList.size() > 0) {
            goodsInfoDAO.updateSaleCountBatch(goodsInfoDOList);
        }
        // 更新订单账目信息
        List<Long> orderIds = new ArrayList<>();
        orderIds.add(orderId);
        updateGoodsOrderAccount(orderIds, AccountStatusEnum.ALREADY_CANCEL);
        // 更新订单账目详情信息
        updateGoodsOrderAccountDetail();
        // 更新订单状态
        Integer version = goodsOrderDAO.getVersionById(orderId);
        GoodsOrderDO goodsOrderDO = GoodsOrderDO.builder().id(orderId).orderStatus(GoodsOrderStatusEnum.CANCELED.getValue().byteValue()).version(version + 1).build();
        return goodsOrderDAO.update(goodsOrderDO);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int confirmOrder(GoodsOrderDTO goodsOrderDTO) {
        return confirm(goodsOrderDTO, null, false);
    }

    @Override
    public void autoConfirmOrders() {
        GoodsOrderAutoJobConfig config = sysConfigService.getByName(SysConfigEnum.GOODS_ORDER_AUTO_JOB_CONFIG.getValue(), GoodsOrderAutoJobConfig.class);
        GoodsOrderQuery goodsOrderQuery = GoodsOrderQuery.builder().orderStatus(GoodsOrderStatusEnum.WAIT_TAKE_DELIVER.getValue().byteValue())
                .deliverTimeMax(DateUtils.millisToDate(DateUtils.currentTimeMillis() - config.getAutoConfirmDays() * 24 * 60 * 60 * 1000)).build();
        PagerDTO pagerDTO = listAllByCondition(goodsOrderQuery);
        List<Object> objList = pagerDTO.getRows();
        log.info("auto confirm goods order count: {}", objList.size());
        for (Object obj : objList) {
            GoodsOrderDTO goodsOrderDTO = (GoodsOrderDTO) obj;
            log.info("auto confirm goods order, order id: {}, order no: {}, deliver time: {}", goodsOrderDTO.getId(), goodsOrderDTO.getOrderNo(), goodsOrderDTO.getDeliverTime());
            int updateRow = confirm(goodsOrderDTO, null, true);
            if (updateRow == 1) {
                log.info("auto confirm done!!!");
            } else {
                log.info("auto confirm error!!!");
            }
        }
    }


    //系统自动取消订单
    @Override
    public void autoCancelOrders() {
        GoodsOrderAutoJobConfig config = sysConfigService.getByName(SysConfigEnum.GOODS_ORDER_AUTO_JOB_CONFIG.getValue(), GoodsOrderAutoJobConfig.class);
        UserGoodsOrderItemQuery  userGoodsOrderItemQuery = UserGoodsOrderItemQuery.builder().goodsOrderOrderStatus(GoodsOrderStatusEnum.WAIT_PAYMENT.getValue().byteValue())
                .goodsOrderCreateTimeMax(DateUtils.millisToDate(DateUtils.currentTimeMillis() - config.getAutoCancelHours() * 60 * 60 * 1000)).build();
        List<Object> objList = userGoodsOrderItemDAO.listAllByCondition(userGoodsOrderItemQuery);

        List<GoodsOrderItemDO> goodsOrderItemDOList = new ArrayList<>();
        Map<Long, Integer> quantityMap = new HashMap<>();
        List<Object> goodsOrderDOList = new ArrayList<>();
        List<Long> orderIds = new ArrayList<>();
        for (Object obj : objList) {
            UserGoodsOrderItemDO userGoodsOrderItemDO = (UserGoodsOrderItemDO) obj;
            log.info("auto cancel goods order, order id: {}, order no: {}, create time: {}", userGoodsOrderItemDO.getGoodsOrderId(), userGoodsOrderItemDO.getGoodsOrderOrderNo(), userGoodsOrderItemDO.getGoodsOrderCreateTime());
            int quantity = userGoodsOrderItemDO.getGoodsOrderItemQuantity();
            goodsOrderItemDOList.add(GoodsOrderItemDO.builder().goodsSkuId(userGoodsOrderItemDO.getGoodsOrderItemGoodsSkuId()).quantity(quantity).build());
            Long goodsId = userGoodsOrderItemDO.getGoodsOrderItemGoodsId();
            if (quantityMap.containsKey(goodsId)) {
                quantityMap.put(goodsId, quantityMap.get(goodsId) + quantity);
            } else {
                quantityMap.put(goodsId, quantity);
            }
            Long orderId = userGoodsOrderItemDO.getGoodsOrderId();
            Integer version = goodsOrderDAO.getVersionById(orderId);
            GoodsOrderDO goodsOrderDO = GoodsOrderDO.builder().id(orderId).orderStatus(GoodsOrderStatusEnum.CANCELED.getValue().byteValue()).version(version + 1).build();
            goodsOrderDOList.add(goodsOrderDO);
            orderIds.add(orderId);
        }
        // 更新商品库存
        if (goodsOrderItemDOList.size() > 0) {
            goodsSkuDAO.updateStockBatch(goodsOrderItemDOList);
        }
        List<GoodsInfoDO> goodsInfoDOList = new ArrayList<>();
        for (Map.Entry<Long, Integer> entry : quantityMap.entrySet()) {
            goodsInfoDOList.add(GoodsInfoDO.builder().id(entry.getKey()).saleCount((long) - entry.getValue()).build());
        }
        // 更新商品销量
        if (goodsInfoDOList.size() > 0) {
            goodsInfoDAO.updateSaleCountBatch(goodsInfoDOList);
        }
        if (orderIds.size()>0) {
            // 更新订单账目信息
            updateGoodsOrderAccount(orderIds, AccountStatusEnum.ALREADY_CANCEL);
        }
        // 更新订单账目详情信息
        updateGoodsOrderAccountDetail();
        if (goodsOrderDOList.size()>0) {
            // 更新订单状态
            goodsOrderDAO.updateBatch(goodsOrderDOList);
        }

    }

    private int confirm(GoodsOrderDTO goodsOrderDTO, Long pickupUserId, Boolean autoConfirm) {
        // 更新订单账目信息
        List<Long> orderIds = new ArrayList<>();
        orderIds.add(goodsOrderDTO.getId());
        updateGoodsOrderAccount(orderIds, AccountStatusEnum.ALREADY_CONFIRM);
        // 更新订单账目详情信息
        updateGoodsOrderAccountDetail();
        // 更新用户账目和平台账目
        updateAccountDetail(goodsOrderDTO.getTransactionNo(), goodsOrderDTO.getPayAmount(), AccountStatusEnum.ALREADY_CONFIRM);
        // 更新订单状态和确认收货（完成）时间
        Integer version = goodsOrderDAO.getVersionById(goodsOrderDTO.getId());
        goodsOrderDTO.setOrderStatus(GoodsOrderStatusEnum.WAIT_COMMENT.getValue().byteValue());
        goodsOrderDTO.setDealTime(DateUtils.currentDate());
        goodsOrderDTO.setVersion(version + 1);
        if (pickupUserId != null) {
            goodsOrderDTO.setShopRemark((goodsOrderDTO.getShopRemark() == null ? "" : goodsOrderDTO.getShopRemark()) + "[提货点核销确认收货，提货点编号：" + pickupUserId + "]");
        } else if (autoConfirm) {
            goodsOrderDTO.setShopRemark((goodsOrderDTO.getShopRemark() == null ? "" : goodsOrderDTO.getShopRemark()) + "[自动确认收货]");
        }
        int updateRows = goodsOrderDAO.update(BeanUtils.copy(goodsOrderDTO, GoodsOrderDO.class));
        if (updateRows == 1) {
            MallCommissionGrantConfig mallCommissionGrantConfig = sysConfigService.getByName(SysConfigEnum.MALL_COMMISSION_GRANT_CONFIG.getValue(), MallCommissionGrantConfig.class);
            if (pickupUserId != null) {
                // 提货点发货
                Long userId = goodsOrderDTO.getUserId();
                distributionService.saveDistributionUserRole(userId, new Long[]{goodsOrderDTO.getId()});
                distributionService.grantCommission(userId, new String[]{goodsOrderDTO.getId() + "-" + goodsOrderDTO.getOrderNo()}, mallCommissionGrantConfig, pickupUserId);
            } else {
                // 用户确认收货或系统自动确认收货
                if (!mallCommissionGrantConfig.getPayOrder() && mallCommissionGrantConfig.getConfirmOrder()) {
                    Long userId;
                    if (autoConfirm) {
                        userId = goodsOrderDTO.getUserId();
                    } else {
                        userId = SecurityUtils.getJwtUser().getUserId();
                    }
                    distributionService.saveDistributionUserRole(userId, new Long[]{goodsOrderDTO.getId()});
                    // 明确设置了付款时不发放而确认收货时发放，则发放佣金
                    distributionService.grantCommission(userId, new String[]{goodsOrderDTO.getId() + "-" + goodsOrderDTO.getOrderNo()}, mallCommissionGrantConfig, null);
                }
            }
        }
        return updateRows;
    }

    private void updateAccountDetail(String transactionNo, BigDecimal payAmount, AccountStatusEnum accountStatusEnum) {
        accountDetailDAO.updateByCondition(AccountDetailDO.builder().transactionNo(transactionNo).amount(payAmount.negate()).accountStatus(accountStatusEnum.getValue().byteValue()).build());
        platformAccountDetailDAO.updateByCondition(PlatformAccountDetailDO.builder().transactionNo(transactionNo).amount(payAmount).accountStatus(accountStatusEnum.getValue().byteValue()).build());
    }

    private void updateGoodsOrderAccount(List<Long> orderIds, AccountStatusEnum accountStatusEnum) {
        List<GoodsOrderAccountDO> goodsOrderAccountDOList = new ArrayList<>();
        List<GoodsOrderAccountDetailDO> goodsOrderAccountDetailDOList = new ArrayList<>();
        for (Long orderId : orderIds) {
            goodsOrderAccountDOList.add(GoodsOrderAccountDO.builder().orderId(orderId).accountStatus(accountStatusEnum.getValue().byteValue()).build());
            goodsOrderAccountDetailDOList.add(GoodsOrderAccountDetailDO.builder().orderId(orderId).accountStatus(accountStatusEnum.getValue().byteValue()).build());
        }
        goodsOrderAccountDAO.updateByOrderIds(goodsOrderAccountDOList);
        goodsOrderAccountDetailDAO.updateByOrderIds(goodsOrderAccountDetailDOList);
    }

    private void updateGoodsOrderAccountDetail() {

    }

    @Override
    public GoodsOrderDTO getByOrderNo(String orderNo) {
        GoodsOrderDO  goodsOrderDO = goodsOrderDAO.getByOrderNo(orderNo);
        return goodsOrderDO == null ? null : BeanUtils.copy(goodsOrderDO, GoodsOrderDTO.class);
    }

    @Override
    public Long countOrderNos(String[] orderNos) {
        return goodsOrderDAO.countOrderNos(orderNos);
    }

    @Override
    public BigDecimal calculateTotalAmount(String[] orderNos) {
        return goodsOrderDAO.calculateTotalAmount(orderNos);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResponseStatusVO goodsOrderDelivery(Long id, GoodsOrderLogisticsVO goodsOrderLogisticsVO) {
        Object obj = getById(id);
        if (null == obj) {
            return ResponseStatusVO.error("订单不存在，发货失败", null);
        }
        Object logisticsObj = goodsOrderLogisticsDAO.getById(id);
        if (null == logisticsObj) {
            return ResponseStatusVO.error("物流信息不存在，发货失败", null);
        }
        // 更新物流信息
        GoodsOrderLogisticsDTO goodsOrderLogisticsDTO = BeanUtils.copy(logisticsObj, GoodsOrderLogisticsDTO.class);
        if (goodsOrderLogisticsDTO.getIsDeliver() == 1) {
            return ResponseStatusVO.error("该订单已发货不能重复发货", null);
        }
        GoodsOrderVO goodsOrderVO = BeanUtils.copy(obj, GoodsOrderVO.class);
        if (goodsOrderVO.getOrderStatus() != GoodsOrderStatusEnum.WAIT_DELIVER.getValue().intValue()) {
            return ResponseStatusVO.error("该订单不是待发货状态，不能发货", null);
        }
        goodsOrderLogisticsDTO.setLogisticsCode(goodsOrderLogisticsVO.getLogisticsCode());
        goodsOrderLogisticsDTO.setLogisticsCompany(goodsOrderLogisticsVO.getLogisticsCompany());
        goodsOrderLogisticsDTO.setLogisticsNo(goodsOrderLogisticsVO.getLogisticsNo());
        goodsOrderLogisticsDTO.setIsDeliver((byte)1);
        int updateRows = goodsOrderLogisticsService.update(goodsOrderLogisticsDTO);
        if (updateRows <= 0) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return ResponseStatusVO.error("更新物流失败，发货失败", null);
        }
        goodsOrderVO.setOrderStatus(GoodsOrderStatusEnum.WAIT_TAKE_DELIVER.getValue().byteValue());
        goodsOrderVO.setDeliverTime(DateUtils.currentDate());
        updateRows = update(goodsOrderVO);
        if (updateRows <= 0) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return ResponseStatusVO.error("更新订单失败，发货失败", null);
        }
        return ResponseStatusVO.ok("发货成功", null);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResponseStatusVO goodsOrderVerification(Long id) {
        Object obj = getById(id);
        if (obj == null) {
            return ResponseStatusVO.dataError("订单不存在", null);
        }
        Integer version = goodsOrderLogisticsDAO.getVersionById(id);
        GoodsOrderLogisticsDO goodsOrderLogisticsDO = GoodsOrderLogisticsDO.builder().id(id).isDeliver((byte) 1).version(version + 1).build();
        goodsOrderLogisticsDAO.update(goodsOrderLogisticsDO);
        GoodsOrderDTO goodsOrderDTO = (GoodsOrderDTO) obj;
        goodsOrderDTO.setDeliverTime(DateUtils.currentDate());
        int updateRow = confirm(goodsOrderDTO, SecurityUtils.getJwtUser().getUserId(), false);
        return updateRow == 1 ? ResponseStatusVO.ok("订单核销成功", null) : ResponseStatusVO.error("订单核销失败", null);

    }

    @Autowired
    public void setGoodsOrderDAO(GoodsOrderDAO goodsOrderDAO) {
        super.setBaseDAO(goodsOrderDAO);
        this.goodsOrderDAO = goodsOrderDAO;
    }

    @Autowired
    public void setGoodsOrderItemDAO(GoodsOrderItemDAO goodsOrderItemDAO) {
        this.goodsOrderItemDAO = goodsOrderItemDAO;
    }

    @Autowired
    public void setGoodsOrderLogisticsDAO(GoodsOrderLogisticsDAO goodsOrderLogisticsDAO) {
        this.goodsOrderLogisticsDAO = goodsOrderLogisticsDAO;
    }

    @Autowired
    public void setGoodsInfoDAO(GoodsInfoDAO goodsInfoDAO) {
        this.goodsInfoDAO = goodsInfoDAO;
    }

    @Autowired
    public void setGoodsSkuDAO(GoodsSkuDAO goodsSkuDAO) {
        this.goodsSkuDAO = goodsSkuDAO;
    }

    @Autowired
    public void setAccountDetailDAO(AccountDetailDAO accountDetailDAO) {
        this.accountDetailDAO = accountDetailDAO;
    }

    @Autowired
    public void setPlatformAccountDetailDAO(PlatformAccountDetailDAO platformAccountDetailDAO) {
        this.platformAccountDetailDAO = platformAccountDetailDAO;
    }

    @Autowired
    public void setGoodsOrderAccountDAO(GoodsOrderAccountDAO goodsOrderAccountDAO) {
        this.goodsOrderAccountDAO = goodsOrderAccountDAO;
    }

    @Autowired
    public void setGoodsOrderAccountDetailDAO(GoodsOrderAccountDetailDAO goodsOrderAccountDetailDAO) {
        this.goodsOrderAccountDetailDAO = goodsOrderAccountDetailDAO;
    }

    @Autowired
    public void setShippingAddressDAO(ShippingAddressDAO shippingAddressDAO) {
        this.shippingAddressDAO = shippingAddressDAO;
    }

    @Autowired
    public void setGoodsOrderLogisticsService(GoodsOrderLogisticsService goodsOrderLogisticsService) {
        this.goodsOrderLogisticsService = goodsOrderLogisticsService;
    }

    @Autowired
    public void setDistributionService(DistributionService distributionService) {
        this.distributionService = distributionService;
    }

    @Autowired
    public void setSysConfigService(SysConfigService sysConfigService) {
        this.sysConfigService = sysConfigService;
    }

    @Autowired
    public void setUserGoodsOrderItemDAO(UserGoodsOrderItemDAO userGoodsOrderItemDAO) {
        this.userGoodsOrderItemDAO = userGoodsOrderItemDAO;
    }

    @PostConstruct
    public void init() {
        super.init(GoodsOrderDO.class, GoodsOrderDTO.class);
    }
}
